/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       positionalsoundsource.cpp
 * Author:     twoja stara
 * Created on: 2009-07-16
 *
 **********************************************************************************************************************/

#include "positionalsoundsource.h"
#include "../system.h"

// well strange, but audio uses the same opengl matrix stack
// until openal has it's own
#include <GL/gl.h>

namespace mGameEngine
{
namespace Audio
{

PositionalSoundSource::PositionalSoundSource() :
    AmbientSoundSource(),
    _refDistance(1.0f),
    _rolloff(1.0f)
{
}

PositionalSoundSource::PositionalSoundSource(    Sound *sound,
                                                float refDistance,
                                                float rolloff) :
    AmbientSoundSource(sound),
    _refDistance(refDistance),
    _rolloff(rolloff)
{
}

PositionalSoundSource::~PositionalSoundSource()
{
}

void PositionalSoundSource::set() const
{
    // sound isn't playing
    if(!isPlaying())
    {
        return;
    }

    // get modelview matrix
    Matrix4x4 modelview;
    glGetFloatv(GL_MODELVIEW_MATRIX, modelview);

    // set sound source
    alSourcef(_source, AL_GAIN, _gain);
    alSourcef(_source, AL_PITCH, 1.0f);
    alSourcefv(_source, AL_POSITION, modelview.m[3]);
    alSourcefv(_source, AL_VELOCITY, Vector3(0.0f, 0.0f, 0.0f));
    alSourcef(_source, AL_REFERENCE_DISTANCE, _refDistance);
    alSourcef(_source, AL_ROLLOFF_FACTOR, _rolloff);
}

}
}

